import { create } from 'zustand';
import { persist } from 'zustand/middleware';
// import { login_router } from '@/router';


interface UserInfo {
  id: number;
  user: string;
  role_id: string;
  user_name: string;
  role_name: string;
}

export interface Permission {
  ps_id: number;
  ps_name: string;
  ps_pid: number;
  ps_level: number;
  id: number;
  ps_api_path: string;
  ps_api_service: string;
  ps_api_order: string;
  children?: Permission[];
}

interface UserState {
  token: string | null;
  userInfo: UserInfo | null;
  isLoggedIn: boolean;
  permissions: Permission[];
  setToken: (token: string) => void;
  setUserInfo: (userInfo: UserInfo) => void;
  setLoggedIn: (isLoggedIn: boolean) => void;
  setPermissions: (permissions: Permission[]) => void;
  logout: () => void;
}

export const useUserStore = create<UserState>()(
  persist(
    (set) => ({
      token: null,
      userInfo: null,
      isLoggedIn: false,
      permissions: [],
      // router: login_router,
      setToken: (token: string) => set({ token }),
      setUserInfo: (userInfo: UserInfo) => set({ userInfo }),
      setLoggedIn: (isLoggedIn: boolean) => set({ isLoggedIn }),
      setPermissions: (permissions: Permission[]) => set({ permissions }),
      logout: () => set({ 
        token: null, 
        userInfo: null, 
        isLoggedIn: false,
        permissions: []
      }),
    }),
    {
      name: 'user-storage',
    }
  )
); 